package cn.tedu.egmvc.controller;

import cn.tedu.egmvc.mapper.AddressMapper;
import cn.tedu.egmvc.pojo.dto.AddressDTO;
import cn.tedu.egmvc.pojo.dto.UpdateAddressDTO;
import cn.tedu.egmvc.pojo.entity.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;
    @RequestMapping("/address/insert")
    public String add(AddressDTO addressDTO){
        System.out.println(addressDTO);
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        System.out.println("address:"+address);
        int num = addressMapper.insertAddress(address);
        return num>0?"添加成功":"添加失败";
    }

    @RequestMapping("/address/select")
    public List<Address> select(){
        return addressMapper.select();
    }

    @RequestMapping("/address/delete")
    public String delete(int id){
        System.out.println("删除id为"+id+"的记录");
        int num = addressMapper.deleteById(1);
        return num>0?"删除成功":"删除失败";
    }

    @RequestMapping("/address/update")
    public String update(UpdateAddressDTO updateAddressDTO){
        Address address = new Address();
        BeanUtils.copyProperties(updateAddressDTO,address);
        int num = addressMapper.update(address);
        return num>0?"修改成功":"修改失败";
    }
}
